package com.wang.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.wang.dao.IBaseDao;
import com.wang.domain.Student;
import com.wang.util.DBCPUtil;

public class StudentDaoImpl implements IBaseDao<Student> {
	public Student findByName(String name){
		Connection connection = null;
		PreparedStatement prepareStatement = null;
		ResultSet executeQuery = null;
		List<Student> arrayList = new ArrayList<Student>();
		try {
			connection = DBCPUtil.getConnection();
			prepareStatement = connection.prepareStatement("select * from student where username=?");
			prepareStatement.setObject(1, name);
			executeQuery = prepareStatement.executeQuery();
			while(executeQuery.next()){
				Student studentUser = new Student();
				studentUser.setId(executeQuery.getLong("id"));
				studentUser.setUsername(executeQuery.getString("username"));
				studentUser.setPassword(executeQuery.getString("password"));
				arrayList.add(studentUser);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBCPUtil.close(executeQuery, prepareStatement, connection);;
		}
		return arrayList.isEmpty()?null:arrayList.get(0);
	}

	@Override
	public Student findOne(Long id) {
		Connection connection = null;
		PreparedStatement prepareStatement = null;
		ResultSet executeQuery = null;
		List<Student> arrayList = new ArrayList<Student>();
		try {
			connection = DBCPUtil.getConnection();
			prepareStatement = connection.prepareStatement("select * from student where id=?");
			prepareStatement.setObject(1, id);
			executeQuery = prepareStatement.executeQuery();
			while(executeQuery.next()){
				Student studentUser = new Student();
				studentUser.setId(executeQuery.getLong("id"));
				studentUser.setUsername(executeQuery.getString("username"));
				studentUser.setPassword(executeQuery.getString("password"));
				arrayList.add(studentUser);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBCPUtil.close(executeQuery, prepareStatement, connection);;
		}
		return arrayList == null?null:arrayList.get(0);
	}

	@Override
	public List<Student> findAll() {
		Connection connection = null;
		PreparedStatement prepareStatement = null;
		ResultSet executeQuery = null;
		List<Student> arrayList = new ArrayList<Student>();
		try {
			connection = DBCPUtil.getConnection();
			prepareStatement = connection.prepareStatement("select * from student");
			executeQuery = prepareStatement.executeQuery();
			while(executeQuery.next()){
				Student studentUser = new Student();
				studentUser.setId(executeQuery.getLong("id"));
				studentUser.setUsername(executeQuery.getString("username"));
				studentUser.setPassword(executeQuery.getString("password"));
				arrayList.add(studentUser);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBCPUtil.close(executeQuery, prepareStatement, connection);;
		}
		return arrayList;
	}

	@Override
	public Integer delete(Long id) {
		Connection connection = null;
		PreparedStatement prepareStatement = null;
		ResultSet executeQuery = null;
		int executeUpdate = -1;
		List<Student> arrayList = new ArrayList<Student>();
		try {
			connection = DBCPUtil.getConnection();
			prepareStatement = connection.prepareStatement("delete from student where id = ?");
			prepareStatement.setObject(1, id);
			executeUpdate = prepareStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBCPUtil.close(executeQuery, prepareStatement, connection);;
		}
		return executeUpdate;
	}

	@Override
	public Integer update(Student t) {
		Connection connection = null;
		PreparedStatement prepareStatement = null;
		ResultSet executeQuery = null;
		int executeUpdate = -1;
		List<Student> arrayList = new ArrayList<Student>();
		try {
			connection = DBCPUtil.getConnection();
			prepareStatement = connection.prepareStatement("update student set username = ?,password = ? where id = ?");
			prepareStatement.setObject(1, t.getUsername());
			prepareStatement.setObject(2, t.getPassword());
			prepareStatement.setObject(3, t.getId());
			executeUpdate = prepareStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBCPUtil.close(executeQuery, prepareStatement, connection);;
		}
		return executeUpdate;
	}

	@Override
	public Integer add(Student t) {
		Connection connection = null;
		PreparedStatement prepareStatement = null;
		ResultSet executeQuery = null;
		int executeUpdate = -1;
		List<Student> arrayList = new ArrayList<Student>();
		try {
			connection = DBCPUtil.getConnection();
			prepareStatement = connection.prepareStatement("insert into student (username,password) values (?,?)");
			prepareStatement.setObject(1, t.getUsername());
			prepareStatement.setObject(2, t.getPassword());
			executeUpdate = prepareStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			DBCPUtil.close(executeQuery, prepareStatement, connection);;
		}
		return executeUpdate;
	}
}
